package com.hb.trade.api.routine.controller.center.au;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.dto.OrgDeptDTO;
import com.hb.core.pojo.center.au.entity.Org;
import com.hb.core.pojo.center.au.entity.OrgDept;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.center.OrgDeptClient;
import com.hb.trade.client.center.UserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe:
 * 部门信息管理
 *
 * @author zhongdl
 * @date 2022/01/20
 */
@Slf4j
@Version(1.0)
@Api(tags = {"部门信息管理"})
@RequestMapping("/{version}")
@RestController
public class ApiOrgDeptController {

    protected final OrgDeptClient orgDeptClient;
    protected final OrgClient orgClient;
    protected final UserClient userClient;

    public ApiOrgDeptController(OrgDeptClient orgDeptClient,
                                OrgClient orgClient,
                                UserClient userClient) {
        this.orgDeptClient = orgDeptClient;
        this.orgClient = orgClient;
        this.userClient = userClient;
    }

    /**
     * @param orgDept:
     * @Description: 分页查询部门列表
     * @Author: zhongdl
     * @Date:2022/1/20 14:35
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "分页查询部门列表", notes = "分页查询部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "企业id", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", dataTypeClass = String.class),

    })
    @PostMapping("/orgDept/pageOrgDept")
    public ResponseDto<?> pageOrgDept(@RequestBody OrgDeptDTO orgDept) {
        Long orgId = orgDept.getOrgId();
        if (orgId == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
/*        //判断一下前端页面传过来的是否是父企业
        Message<List<OrgDTO>> listMessage = orgClient.listParent(ShiroUtil.getOrgId());

        if (listMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        if (CollUtil.isNotEmpty(listMessage.getData())) {
            List<Long> parentOrgIdList = listMessage.getData().stream().map(OrgDTO::getId).collect(Collectors.toList());
            //如果前端传过来的是父级企业只查询当前登录账号企业下面的部门
            orgId = CollUtil.contains(parentOrgIdList, orgId) ? ShiroUtil.getOrgId() : orgId;
        }*/

        orgDept.setRoleSign(ShiroUtil.getExtInfoInt("roleSign"));
        Message<PageResult<OrgDeptDTO>> pageRoleMsg = orgDeptClient.pageOrgDept(orgDept);
        return ResultUtil.msg(pageRoleMsg);
    }

    @ApiOperation(value = "查询企业部门列表", notes = "查询企业部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "企业id", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", dataTypeClass = String.class),

    })
    @PostMapping("/orgDept/listOrgDept")
    public ResponseDto<?> listOrgDept(@RequestBody OrgDeptDTO orgDept) {
        Long orgId = orgDept.getOrgId();
        if (orgId == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
/*        //判断一下前端页面传过来的是否是父企业
        Message<List<OrgDTO>> listMessage = orgClient.listParent(ShiroUtil.getOrgId());

        if (listMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        if (CollUtil.isNotEmpty(listMessage.getData())) {
            List<Long> parentOrgIdList = listMessage.getData().stream().map(Org::getId).collect(Collectors.toList());
            //如果前端传过来的是父级企业只查询当前登录账号企业下面的部门
            orgId = CollUtil.contains(parentOrgIdList, orgId) ? ShiroUtil.getOrgId() : orgId;
        }*/
        orgDept.setOrgId(orgId);
        orgDept.setRoleSign(ShiroUtil.getExtInfoInt("roleSign"));
        Message<List<OrgDept>> pageRoleMsg = orgDeptClient.listOrgDept(orgDept);
        return ResultUtil.msg(pageRoleMsg);
    }

    /**
     * @param orgDept:
     * @Description: 保存部门
     * @Author: zhongdl
     * @Date:2022/1/20 16:09
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "保存部门", notes = "保存部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "企业id", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "manageId", value = "负责人id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dutyName", value = "负责人", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dutyTel", value = "负责人联系方式", dataTypeClass = String.class),

    })
    @PostMapping("/orgDept/saveOrgDept")
    public ResponseDto<?> saveOrgDept(@RequestBody OrgDeptDTO orgDept) {
        // 参数验证
        String paramsValid = orgDept.paramsValid();
        if (StrUtil.isNotEmpty(paramsValid)) {
            return ResultUtil.fail(paramsValid);
        }

        orgDept.setCreateId(ShiroUtil.getUserId());

        Long manageId = orgDept.getManageId();
        if (manageId != null) {
            Message<User> userMessage = userClient.getByUserId(manageId);
            if (userMessage.isSuccess()) {
                User data = userMessage.getData();
                orgDept.setDutyName(data.getFullName());
                orgDept.setDutyTel(data.getPhone());
            }
        }

        Message<String> booleanMessage = orgDeptClient.saveOrgDept(orgDept);
        //查询下部门名称是否重复
        if (booleanMessage.isFail() && StrUtil.isNotEmpty(booleanMessage.getMessage())) {
            return ResultUtil.fail(booleanMessage.getMessage());
        }
        if (booleanMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        return ResultUtil.msg(booleanMessage);
    }


    /**
     * @param orgDept:
     * @Description: 根据id查询部门详情
     * @Author: zhongdl
     * @Date:2022/1/20 16:09
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "根据id查询部门详情", notes = "根据id查询部门详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class, required = true),
    })
    @PostMapping("/orgDept/getOrgDeptId")
    public ResponseDto getOrgDeptId(@RequestBody OrgDeptDTO orgDept) {
        if (orgDept.getId() == null) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        Message<OrgDeptDTO> booleanMessage = orgDeptClient.getOrgDeptId(orgDept);
        if (booleanMessage.isFail()) {
            return ResultUtil.FAIL;
        }
        return ResultUtil.msg(booleanMessage);
    }

    /**
     * @param orgDept:
     * @Description: 根据id删除部门
     * @Author: zhongdl
     * @Date:2022/1/20 16:09
     * @return: {@link ResponseDto }
     **/
    @ApiOperation(value = "根据id删除部门", notes = "根据id删除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class, required = true),
    })
    @PostMapping("/orgDept/removeById")
    public ResponseDto<?> removeById(@RequestBody OrgDeptDTO orgDept) {
        if (orgDept.getId() == null) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        Message<Boolean> booleanMessage = orgDeptClient.removeById(orgDept);
        return ResultUtil.msg(booleanMessage);
    }
}
